package com.wuge.gulimall.coupon.service.impl;

import com.wuge.common.to.MemberPrice;
import com.wuge.common.to.SkuRedectionTo;
import com.wuge.gulimall.coupon.entity.MemberPriceEntity;
import com.wuge.gulimall.coupon.entity.SkuLadderEntity;
import com.wuge.gulimall.coupon.service.MemberPriceService;
import com.wuge.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.Query;

import com.wuge.gulimall.coupon.dao.SkuFullReductionDao;
import com.wuge.gulimall.coupon.entity.SkuFullReductionEntity;
import com.wuge.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
  @Autowired
  private    SkuLadderService skuLadderService;
  @Autowired
  MemberPriceService memberPriceService;

    @Override
    @Transactional
    public void saveRel(SkuRedectionTo skuRedectionTo) {
        SkuLadderEntity skuLadderEntity=new SkuLadderEntity();
        skuLadderEntity.setDiscount(skuRedectionTo.getDiscount());
        skuLadderEntity.setAddOther(skuRedectionTo.getCountStatus());
        skuLadderEntity.setFullCount(skuRedectionTo.getFullCount());
        skuLadderEntity.setSkuId(skuRedectionTo.getSkuId());
        if (skuLadderEntity.getFullCount()>0){
            skuLadderService.save(skuLadderEntity);
        }

        SkuFullReductionEntity skuFullReductionEntity=new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuRedectionTo,skuFullReductionEntity);
        if (skuFullReductionEntity.getReducePrice()!=null&&skuFullReductionEntity.getReducePrice().compareTo(BigDecimal.ZERO)==1){
            save(skuFullReductionEntity);
        }
        List<MemberPrice> memberPrice = skuRedectionTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntityList = memberPrice.stream().filter(
                (entity)->{
                    return entity.getPrice().compareTo(BigDecimal.ZERO)==1;
                }
        ).map((member) -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setAddOther(1);
            memberPriceEntity.setMemberLevelId(member.getId());
            memberPriceEntity.setMemberLevelName(member.getName());
            memberPriceEntity.setSkuId(skuRedectionTo.getSkuId());
            memberPriceEntity.setMemberPrice(member.getPrice());
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntityList);

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

}